package com.example.smartstart.service.impl;

import com.example.smartstart.entity.Award;
import com.example.smartstart.entity.AwardRecord;
import com.example.smartstart.entity.Point;
import com.example.smartstart.mapper.AwardMapper;
import com.example.smartstart.mapper.AwardRecordMapper;
import com.example.smartstart.mapper.PointMapper;
import com.example.smartstart.service.IAwardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartstart.service.IPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dlx
 * @since 2024-07-22
 */
@Service
public class AwardServiceImpl extends ServiceImpl<AwardMapper, Award> implements IAwardService {

    @Autowired
    private AwardMapper awardMapper;

    @Autowired
    private AwardRecordMapper awardRecordMapper;

    @Autowired
    private PointMapper pointMapper;

    @Override
    public Integer queryTotal(Award award) {
        return awardMapper.queryTotal(award);
    }

    @Override
    public List<Award> listByPage(Award award) {
        return awardMapper.listByPage(award);
    }

    @Override
    public Integer deleteMoreAward(Integer[] ids) {
        return awardMapper.deleteBatchIds(Arrays.stream(ids).toList());
    }

    @Override
    @Transactional
    public void exchangeAward(Integer id) {
        Integer userId = 1;// token里取

        Award award = awardMapper.selectById(id);
        Integer awardId = id;
        Integer inventory = award.getInventory()-1;
        Integer usePoints = award.getUsePoints();


        // 先判断积分是否足够
        Point point =pointMapper.selectById(userId);
        Integer havePoints = point.getPointCount();
        if (havePoints<usePoints){
            throw new InsufficientPointsException("积分不足，无法执行操作");
        }
        // 判断库存是否足够
        if (inventory<0){
            throw new InsufficientStockException("库存不足，无法执行操作");
        }

        Award newAward = new Award();
        newAward.setId(id);
        newAward.setInventory(inventory);

        AwardRecord awardRecord = new AwardRecord();
        awardRecord.setAwardId(awardId);
        awardRecord.setUserId(userId);
        awardRecord.setCreateTime(LocalDateTime.now());

        point.setPointCount(havePoints-usePoints);

        try{
            // 更新奖励表库存
            awardMapper.updateById(newAward);
            // 添加记录表
            awardRecordMapper.insert(awardRecord);
            // 修改用户积分表
            pointMapper.updateById(point);
        } catch (Exception e) {
            throw new RuntimeException("执行过程中发生错误", e);
        }



    }

    // 自定义运行时异常
    public static class InsufficientPointsException extends RuntimeException {
        // 使用传入的消息创建异常
        public InsufficientPointsException(String message) {
            super(message);
        }
    }

    // 自定义运行时异常
    public static class InsufficientStockException extends RuntimeException {
        // 使用传入的错误消息创建异常
        public InsufficientStockException(String message) {
            super(message);
        }

    }
}
